﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="AddTaskCommand.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace HpcSchedulerManagement.Commands
{
    using System;

    using Common;

    using HpcSchedulerManagement.DataContracts;

    using RestClient;

    public class AddTaskCommand : HpcSchedulerCommandBase
    {
        private readonly string hpcClusterName;

        private readonly int jobId;

        private readonly SchedulerTask task;

        public AddTaskCommand(IHpcSchedulerServiceContext serviceContext, string hpcClusterName, int jobId, SchedulerTask task) : base(serviceContext)
        {
            if (string.IsNullOrEmpty(hpcClusterName))
            {
                throw new ArgumentException("Hpc cluster name must not be null or empty.", "hpcClusterName");
            }

            if (jobId <= 0)
            {
                throw new ArgumentException("Job id must not be less than or equal to 0.", "jobId");
            }

            if (task == null)
            {
                throw new ArgumentNullException("task", "Task must not be null.");
            }

            this.hpcClusterName = hpcClusterName;
            this.jobId = jobId;
            this.task = task;
        }

        protected override RequestContext GetRequestContext()
        {
            return this.CreatePostOperationRequestContext();
        }

        protected override string FormatPath(IHpcSchedulerServiceContext serviceContext)
        {
            return string.Format("{0}/Job/{1}/Tasks", this.hpcClusterName, this.jobId);
        }

        protected override string FormatBody()
        {
            return XmlHelper.Serialize(HpcDataContractsTranslator.TaskToArrayOfPropeties(this.task));
        }
    }
}